package com.campus.customer.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campus.common.bean.common.PageUtil;
import com.campus.customer.bean.notice.NoticeQueryParam;
import com.campus.customer.entity.Notice;
import com.campus.customer.mapper.NoticeMapper;
import com.campus.customer.vo.NoticeVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 公告表 服务实现类
 * </p>
 *
 * @author ZhaoYuJie
 * @since 2025-07-15
 */
@Service
public class NoticeService extends ServiceImpl<NoticeMapper, Notice> {

    /**
     * 获取顾客端公告列表（首页显示）
     * @return 公告列表
     */
    public List<NoticeVo> getCustomerNoticeList() {
        LambdaQueryWrapper<Notice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Notice::getStatus, 1) // 启用状态
                .and(wrapper -> wrapper.eq(Notice::getTargetType, 0) // 全部端
                        .or().eq(Notice::getTargetType, 1)) // 顾客端
                .le(Notice::getStartTime, LocalDateTime.now()) // 开始时间小于等于当前时间
                .ge(Notice::getEndTime, LocalDateTime.now()) // 结束时间大于等于当前时间
                .orderByDesc(Notice::getIsTop) // 置顶优先
                .orderByDesc(Notice::getPriority) // 优先级高的在前
                .orderByDesc(Notice::getCreateTime); // 创建时间倒序
        
        List<Notice> noticeList = this.list(queryWrapper);
        return noticeList.stream()
                .map(notice -> {
                    NoticeVo vo = new NoticeVo();
                    BeanUtil.copyProperties(notice, vo);
                    return vo;
                })
                .collect(Collectors.toList());
    }

    /**
     * 分页查询公告列表
     * @param queryParam 查询参数
     * @return 分页结果
     */
    public PageInfo<NoticeVo> getNoticeListPage(NoticeQueryParam queryParam) {
        PageInfo<Notice> pageInfo = PageHelper.startPage(queryParam.getPageNo(), queryParam.getPageSize())
                .doSelectPageInfo(() -> getNoticeList(queryParam));
        
        List<NoticeVo> voList = Lists.newArrayList();
        for (Notice notice : pageInfo.getList()) {
            NoticeVo vo = new NoticeVo();
            BeanUtil.copyProperties(notice, vo);
            voList.add(vo);
        }
        
        return PageUtil.convertPageInfo(pageInfo, voList);
    }
    
    /**
     * 获取公告列表（不分页）
     * @param queryParam 查询参数
     * @return 公告列表
     */
    private List<Notice> getNoticeList(NoticeQueryParam queryParam) {
        LambdaQueryWrapper<Notice> queryWrapper = new LambdaQueryWrapper<>();
        
        // 构建查询条件
        if (queryParam.getTitle() != null && !queryParam.getTitle().trim().isEmpty()) {
            queryWrapper.like(Notice::getTitle, queryParam.getTitle().trim());
        }
        if (queryParam.getTargetType() != null) {
            queryWrapper.eq(Notice::getTargetType, queryParam.getTargetType());
        }
        if (queryParam.getPriority() != null) {
            queryWrapper.eq(Notice::getPriority, queryParam.getPriority());
        }
        if (queryParam.getStatus() != null) {
            queryWrapper.eq(Notice::getStatus, queryParam.getStatus());
        }
        if (queryParam.getIsTop() != null) {
            queryWrapper.eq(Notice::getIsTop, queryParam.getIsTop());
        }
        
        // 排序
        queryWrapper.orderByDesc(Notice::getIsTop)
                .orderByDesc(Notice::getPriority)
                .orderByDesc(Notice::getCreateTime);
        
        return this.list(queryWrapper);
    }

    /**
     * 根据ID获取公告详情
     * @param id 公告ID
     * @return 公告详情
     */
    public NoticeVo getNoticeById(Integer id) {
        Notice notice = this.getById(id);
        if (notice == null) {
            return null;
        }
        
        NoticeVo vo = new NoticeVo();
        BeanUtil.copyProperties(notice, vo);
        return vo;
    }

    /**
     * 增加公告查看次数
     * @param id 公告ID
     * @return 是否成功
     */
    public boolean incrementViewCount(Integer id) {
        return this.baseMapper.incrementViewCount(id) > 0;
    }
}
